package com.china08.yxyapi.api.home.activity.healthcompetition;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.huodong.healthcompetition.YxyCompetitionQues;
import com.china08.yxyapi.entity.mg.huodong.healthcompetition.YxyCompetitionStudent;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.huodong.YxyCompetitionQuesRepository;
import com.china08.yxyapi.repository.mg.huodong.YxyCompetitionStudentRepository;

/**
 * 点击上一题 下一题 或 题号触发 查找题目
 * 
 * @author zd.Jin
 *
 *
 */
@RestController
@CrossOrigin
public class QuesSrhController extends YxyApiBaseController {
	@Autowired
	private YxyCompetitionQuesRepository yxyCompetitionQuesRepository;
	@Autowired
	private YxyCompetitionStudentRepository yxyCompetitionStudentRepository;

	@RequestMapping(value = "/api_yw/home/activity/healthcompetition/studentcard/quessrh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
		RespModel respModel = new RespModel();
		YxyCompetitionStudent yxyCompetitionStudent = yxyCompetitionStudentRepository
				.findTop1ByStudentId(reqModel.studentId);
		if (yxyCompetitionStudent != null) {
			String stage = yxyCompetitionStudent.getStage();
			String stageno = "";
			if ("小学".equals(stage)) {
				respModel.wenjuan = "小学生健康教育知识竞赛试题";
				stageno = "01";
			} else if ("初中".equals(stage)) {
				respModel.wenjuan = "初高中学生健康教育知识竞赛试题";
				stageno = "02";
			} else if ("高中".equals(stage)) {
				respModel.wenjuan = "初高中学生健康教育知识竞赛试题";
				stageno = "03";
			}
			YxyCompetitionQues yxyCompetitionQues = yxyCompetitionQuesRepository.findTop1ByStageAndQuesNo(stageno,
					reqModel.quesOrder);
			// 题干 选项
			respModel.quesOrder = reqModel.quesOrder;
			respModel.questionBody = yxyCompetitionQues.getBody();
			if (yxyCompetitionQues.getQuesOptions() != null) {
				respModel.quesOptions = yxyCompetitionQues.getQuesOptions().split("#");
			}
		}
		return respModel;
	}

	protected static class ReqModel extends YxyApiPageReqModel {

		public Integer quesOrder;// 题目order
		public String studentId;// 学生id

		public void check() {
			Assert.notNull(StringUtils.defaultIfBlank(studentId, null), "学生id不能为空");
		}

	}

	// 家长身份远程调用接口返回学生id集合
	protected static class ParRespModel {
		public List<String> studentInfos;
	}

	// 题目回答情况实体类
	protected static class RespModel extends YxyApiRespModel {
		// 问卷名称
		public String wenjuan;
		// 题号
		public Integer quesOrder;

		// 正确答案
		// public String quesAnswer;
		// 试题body
		public String questionBody;
		// 试题选项
		public String[] quesOptions;
	}
}
